package com.liuqi.business.service.impl;


import com.liuqi.base.BaseMapper;
import com.liuqi.base.BaseServiceImpl;
import com.liuqi.business.constant.KeyConstant;
import com.liuqi.business.enums.ProtocolEnum;
import com.liuqi.business.enums.SwitchEnum;
import com.liuqi.business.mapper.CurrencyExtractMapper;
import com.liuqi.business.model.CurrencyExtractModel;
import com.liuqi.business.model.CurrencyExtractModelDto;
import com.liuqi.business.service.CurrencyExtractService;
import com.liuqi.business.service.CurrencyService;
import com.liuqi.redis.RedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true)
public class CurrencyExtractServiceImpl extends BaseServiceImpl<CurrencyExtractModel,CurrencyExtractModelDto> implements CurrencyExtractService{

	@Autowired
	private CurrencyExtractMapper currencyExtractMapper;
	@Autowired
	private RedisRepository redisRepository;
	@Autowired
	private CurrencyService currencyService;
	@Override
	public BaseMapper<CurrencyExtractModel,CurrencyExtractModelDto> getBaseMapper() {
		return this.currencyExtractMapper;
	}


	@Override
	protected void doMode(CurrencyExtractModelDto dto) {
		super.doMode(dto);
		dto.setCurrencyName(currencyService.getNameById(dto.getCurrencyId()));
		dto.setProtocolStr(ProtocolEnum.getName(dto.getProtocol()));
	}

	@Override
	public CurrencyExtractModelDto getById(Long id) {
		String key= KeyConstant.KEY_EXTRACT_ID+id;
		CurrencyExtractModelDto extract = redisRepository.getModel(key);
		if (extract == null) {
			extract=currencyExtractMapper.getById(id);
			if(extract!=null){
				redisRepository.set(key, extract, 1L, TimeUnit.DAYS);
			}
		}
		return extract;
	}

	@Override
	public List<CurrencyExtractModelDto> getExtractList() {
		String key= KeyConstant.KEY_EXTRACT_LIST;
		List<CurrencyExtractModelDto> list=redisRepository.getModel(key);
		if(list==null){
			list=this.queryListByDto(new CurrencyExtractModelDto(),true);
			if(list!=null){
				redisRepository.set(key,list,10L, TimeUnit.DAYS);
			}
		}
		return list;
	}

	@Override
	public List<CurrencyExtractModelDto> getOnExtractList() {
		List<CurrencyExtractModelDto> list=this.getExtractList();
		return list.stream().filter(t-> SwitchEnum.isOn(t.getExtractSwitch())).collect(Collectors.toList());
	}


	@Override
	public CurrencyExtractModelDto getByCurrencyAndProtocol(Long currencyId, Integer protocol) {
		return currencyExtractMapper.getByCurrencyAndProtocol(currencyId,protocol);
	}

	@Override
	public void cleanCacheByModel(CurrencyExtractModel currencyExtractModel) {
		super.cleanCacheByModel(currencyExtractModel);
		String key= KeyConstant.KEY_EXTRACT_ID+currencyExtractModel.getId();
		redisRepository.del(key,KeyConstant.KEY_EXTRACT_LIST);
	}

	@Override
	public void cleanAllCache() {
		super.cleanAllCache();
		String key= KeyConstant.KEY_EXTRACT_LIST;
		redisRepository.del(key);
	}
}
